package ro.msg.quiz.repository;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.InputStream;
import java.net.URL;

public class ImportXml
{
	// This method read questions.xml
	public void readFromXmlFile()
	{
		try
		{

			URL xmlURL = new URL("https://project-questionnaire.googlecode.com/svn/trunk/ProjectQuestionnaireAdmin/doc/examples/questions.xml");
			InputStream xml = xmlURL.openStream();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(xml);
			xml.close();

			doc.getDocumentElement().normalize();

			System.out.println("Root element : " + doc.getDocumentElement().getNodeName());

			NodeList nListQ = doc.getElementsByTagName("question");

			System.out.println("------------------------------------------------------------------");

			for (int temp = 0; temp < nListQ.getLength(); temp++)
			{

				Node nNode = nListQ.item(temp);
				Node node = nListQ.item(temp);

				System.out.println("Question " + temp );

				if (nNode.getNodeType() == Node.ELEMENT_NODE)
				{
					Element eElement = (Element) nNode;

					System.out.println("id " + eElement.getElementsByTagName("id").item(0).getTextContent());
					System.out.println("type " + eElement.getElementsByTagName("type").item(0).getTextContent());
					System.out.println("text " + eElement.getElementsByTagName("text").item(0).getTextContent());
                    // for singleChoiceQ
					if( Integer.parseInt(eElement.getElementsByTagName("type").item(0).getTextContent()) == 1 )
                        {
                            for(int i = 0; i < 4; i++ )
                            {
                                System.out.println("Answer " + i + eElement.getElementsByTagName("answer").item(i).getTextContent());
                            }

                            System.out.println("Correct answer: "  + eElement.getElementsByTagName("correct_answer").item(0).getTextContent());
                        }
                    else
                        {
                    	    // for multipleChoiceQ
                            if( Integer.parseInt(eElement.getElementsByTagName("type").item(0).getTextContent()) == 2 )
                            {
                                  for(int i = 0; i < 4; i++ )
                                  {
                                     System.out.println("Answer " + i + eElement.getElementsByTagName("answer").item(i).getTextContent());
                                  }



                               /* NodeList nListCorrect = doc.getElementsByTagName("correct_answer");
                                int sizeListCorrect = nListCorrect.getLength();

                                for( int i=0; i < sizeListCorrect; i++ )
                                {
                                    System.out.print("Correct answer: "  + eElement.getElementsByTagName("correct_answer").item(i).getTextContent() );

                                }
                                System.out.println();*/
                            }

                           else
                            {
                        	   // for textQ
                                System.out.print("Correct answer: "  + eElement.getElementsByTagName("correct_answer").item(0).getTextContent());
                            }
                        }
                }


			}
    }
		catch (Exception e)
		{
			e.printStackTrace();
		}
  }

}
